/**
 ******************************************************************************
 * @file    gpio.c
 * @author  ��Ӿ��
 * @date    2025/5/29
 * @version v1.0
 * @brief   STM32F4 GPIO ��ʼ���ļ�
 *          �ṩGPIO��ʼ����ؽӿ�������
 * @copyright Copyright (c) 2025
 * @note    ������ ��20 ��Ŀ������HAL�⿪����
 ******************************************************************************
 */
#include "app_key.h"
#include "bsp_key.h"
#include "bsp_motor_driver.h"

void set_dac_voltage(float voltage);
void key_task()
{
	static uint8_t init_flag = 0;
	static uint8_t key_down, key_up, key_val, key_old;
	static float voltage = 1.65;
	
	// �ⲿʹ��
	static uint16_t motor_angle = 0;
	if(init_flag == 0)
	{
		init_flag = 1;
		btn_init();
	}
	else
	{
//		btn_task();
		key_val  = key_scan();
		key_down = key_val&(key_val^key_old);
		key_old  = key_val;
		switch(key_down)
		{
			case 1:
				g_led_buf[0] = ~g_led_buf[0];
				g_dac_mode  ^= 1;
				if(motor_angle < 360) motor_angle += 10;
				voltage+=0.1;
				set_dac_voltage(voltage);
			break;
			case 2:
				g_motor_tims = motor_angle;
				extern MotorHandle_t 	 g_my_motor;
				g_my_motor.step_count = 0;
				motor_angle  = 0;
				g_led_buf[1] = ~g_led_buf[1];
				voltage-=0.1;
				set_dac_voltage(voltage);
			break;
		}
	}
}

// ����Ҫ�޸ĵ�ѹ�ĵط�ֱ�Ӹ�������Ԫ��
void set_dac_voltage(float voltage)
{
    // ����VREF+=3.3V��12λDAC
    uint32_t dac_value = (uint32_t)(voltage / 3.3f * 4095);
//    g_dac_buf[0] = dac_value; // ֱ���޸Ļ�����ֵ
}
